﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Composite.Presentation.Regions;
using System.Windows.Controls;
using Microsoft.Practices.Unity;
using System.Windows.Forms.Integration;
using Microsoft.Practices.Composite.Regions;
using System.Windows.Controls.Primitives;
using Microsoft.Practices.Composite.Presentation.Regions.Behaviors;

namespace Infrastructure.WpfClient.Composite.Regions.TabRegion
{
    public class TabRegionAdapter : RegionAdapterBase<TabControl>
    {
        [Dependency]
        public IUnityContainer Container { get; set; }
          public TabRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
              : base(regionBehaviorFactory)
        {
        
        }
        
        protected override Microsoft.Practices.Composite.Regions.IRegion CreateRegion()
        {
            return Container.Resolve<TabViewRegion>();
        }
        protected override void Adapt(Microsoft.Practices.Composite.Regions.IRegion region, TabControl regionTarget)
        {            
            //region.ActiveViews.CollectionChanged += (sender, e) =>
            //{
            //    switch (e.Action)
            //    {
            //        case System.Collections.Specialized.NotifyCollectionChangedAction.Add:

            //            var tab = new TabItem();                       
            //            var view = region.ActiveViews.FirstOrDefault();
                        
            //            if (view != null)
            //            {

            //                var attview = Attribute.GetCustomAttribute(view.GetType(), typeof(Infrastructure.Aspect.Metadata.ViewMetadata)) as Infrastructure.Aspect.Metadata.ViewMetadata;
            //                if (attview != null)
            //                {
            //                    tab.Header = attview.Titulo;
            //                }

            //                if (view is System.Windows.Forms.UserControl)
            //                {
            //                    var host = new WindowsFormsHost();
            //                    host.Child = view as System.Windows.Forms.UserControl;
            //                    tab.Content = host;
            //                }
            //                else
            //                {
            //                    tab.Content = view;
            //                }                            
            //            }                        
            //            regionTarget.Items.Add(tab);                        
            //            regionTarget.Visibility = System.Windows.Visibility.Visible;                            
            //            break;
            //        case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
            //            break;
            //        case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                        
            //            break;
            //        case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
            //            break;
            //        case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
            //            break;
            //        default:
            //            break;
            //    }
                
            //};
            
            //region.Views.CollectionChanged += (sender , e)=>
            //{
            //    switch (e.Action)
            //    {
            //        case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
            //            region.Activate(e.NewItems[0]);
            //        break;                                        
            //        case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
            //            var oview = e.OldItems[0];
            //            TabItem oviewexp = null;
            //            foreach (TabItem item in regionTarget.Items)
            //            {
            //                if (item.Content == oview)
            //                {
            //                    oviewexp = item;
            //                }                                                        
            //            }
            //            regionTarget.Items.Remove(oviewexp);                        
            //        break;
            //    }
            //};

        }
        protected override void AttachBehaviors(IRegion region, TabControl regionTarget)
        {
            // Add the behavior that syncs the items source items with the rest of the items
            region.Behaviors.Add(TabControlItemsSourceSyncBehavior.BehaviorKey, new TabControlItemsSourceSyncBehavior()
            {
                HostControl = regionTarget
            });            
            base.AttachBehaviors(region, regionTarget);
        }
    }
}
